<?php
Globals::requireClass('Controller');
Globals::requireClass('Alipay');
Globals::requireModel('User');
Globals::requireTable('User');
Globals::requireTable('Vlevel');
Globals::requireTable('Vmoney');
Globals::requireTable('Vmoneyimg');
Globals::requireTable('Vmoneyproduct');
Globals::requireTable('Vmoneyorder');

class VmoneyController extends Controller
{
    protected $UserTable;
    protected $VlevelTable;
    protected $VmoneyTable;
	protected $VmoneyimgTable;
	protected $VmoneyproductTable;
	protected $VmoneyorderTable;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> true,
		'layoutEnabled'	=> true,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
		parent::__construct($config);
		$this->UserTable = new UserTable($config);
		$this->VlevelTable = new VlevelTable($config);
		$this->VmoneyTable = new VmoneyTable($config);
		$this->VmoneyimgTable = new VmoneyimgTable($config);
		$this->VmoneyproductTable = new VmoneyproductTable($config);
		$this->VmoneyorderTable = new VmoneyorderTable($config);
		dolog(json_encode($_GET));
	}
	
	public function indexAction()
	{
		$this->layout->pageTitle	= "test";
	}
	
	public function vmoneyrechargeAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','list'=>array()));
	        exit();
	    }
	    
	    $re = array(
	        'imagelist' => array(),
	        'vmoney' => '0',
	        'vrank' => '0',
	        'vrankdiscount' => '0',
	        'lokvalue' => '0',
	        'lokvaluediscount' => '0',
	        'vrechargelist' => array()
	    );
	    
	    $imgdata = $this->VmoneyimgTable->listAll();
	    foreach ($imgdata as $v){
	        $tmp = array();
	        $tmp['imageurl'] = (string)DOMAIN.str_replace('../', '', $v['img']);
	        $tmp['url'] = $v['url'];
	        $re['imagelist'][] = $tmp;
	    }
	    
	    $vcount = 0;
	    $listvalue = $this->VmoneyTable->listAllWithFields('money',array('uid'=>$uid));
	    foreach ($listvalue as $k=>$v){
	        $vcount += $v['money'];
	    }
	    $re['vmoney'] = $vcount;
	    
	    $vlevel = 0;
	    $listvalue = $this->VlevelTable->listAllWithFields('level',array('uid'=>$uid));
	    foreach ($listvalue as $k=>$v){
	        $vlevel += $v['level'];
	    }
	    $re['vrank'] = $vlevel;
	    
	    if($vlevel < 100){
	        $re['vrankdiscount'] = '0.98';
	    }elseif($vlevel >= 100 && $vlevel < 500){
	        $re['vrankdiscount'] = '0.97';
	    }elseif($vlevel >= 500){
	        $re['vrankdiscount'] = '0.96';
	    }
	    
	    $re['lokvalue'] = $uid;
	    
	    if($uid < 10000){
	        $re['lokvaluediscount'] = '0.98';
	    }elseif($uid >= 10000){
	        $re['lokvaluediscount'] = '0.99';
	    }
	    
	    $maindata = $this->VmoneyproductTable->listAll(array('status'=>1));
	    foreach ($maindata as $v){
	        $tmp = array();
	        $tmp['vvalue'] = $v['vmoney_count'];
	        $tmp['vprice'] = round($v['vmoney_count'] * $re['vrankdiscount'] * $re['lokvaluediscount'],2);
	        $tmp['vid'] = $v['id'];
	        $re['vrechargelist'][] = $tmp;
	    }
	    
	    echo json_encode($re);
	    exit();
	}
	
	public function payvmAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    $user = $this->UserTable->getRow($uid);
	    if($uid < 1 || $user['id'] < 1){
	        echo json_encode(array('returncode'=>-1,'orderid'=>''));
	        exit();
	    }
	    
	    $time = time();
	    $vid = $this->getIntParam('vid');
	    $main = $this->VmoneyproductTable->getRow($vid);
	    if(empty($vid) || $main['status'] != 1){
	        echo json_encode(array('returncode'=>-2,'orderid'=>''));
	        exit();
	    }
	    
	    $time_day = date('Y-m-d H:i:s');
	    
	    $vlevel = 0;
	    $listvalue = $this->VlevelTable->listAllWithFields('level',array('uid'=>$uid));
	    foreach ($listvalue as $k=>$v){
	        $vlevel += $v['level'];
	    }
	    if($vlevel < 100){
	        $vrankdiscount = 0.98;
	    }elseif($vlevel >= 100 && $vlevel < 500){
	        $vrankdiscount = 0.97;
	    }elseif($vlevel >= 500){
	        $vrankdiscount = 0.96;
	    }
	    
	    if($uid < 10000){
	        $lokvaluediscount = 0.98;
	    }elseif($uid >= 10000){
	        $lokvaluediscount = 0.99;
	    }
	    
	    $money_value = round($main['vmoney_count'] * $vrankdiscount * $lokvaluediscount,2);
	    
	    $data['orderid'] = substr($time, -9) . mt_rand(10, 99);
	    $data['uid'] = $uid;
	    $data['vid'] = $vid;
	    $data['createtime'] = date('Y-m-d H:i:s');
	    $data['status'] = 0;
	    $data['money'] = $money_value;
	    $this->VmoneyorderTable->add($data);
	    echo json_encode(array('returncode'=>0,'orderid'=>(string)$data['orderid']));
	    exit();
	}
	
	public function payvmokAction(){
	    $orderid = $this->getParam('orderid');
	    $payorderid = $this->getParam('payorderid');
	    $paytype = $this->getParam('paytype');
	    $sign = $this->getParam('sign');
	    $time = time();
	    
	    $order = $this->VmoneyorderTable->getRow(array('orderid'=>$orderid));
	    $main = $this->VmoneyproductTable->getRow($order['vid']);
	    $user_buyer = $this->UserTable->getRow($order['uid']);
	    
	    if($user_buyer['id'] < 1 || $order['id'] < 1 || $order['status'] != 0 || $main['id'] < 1 || $main['status'] != 1){
	        echo json_encode(array('returncode'=>-1));
	        exit();
	    }
	    /*
	    $new_num_arr = array();
	    $orderid_num = $orderid + 19921206;
	    for ($ii = 0 ; $ii < strlen($orderid_num) ; $ii++){
	        $tmp_num = substr($orderid_num, $ii , 1);
	        $new_num_arr[] = $tmp_num;
	    }
	    krsort($new_num_arr);
	    $new_num = '';
	    foreach ($new_num_arr as $v){
	        $new_num .= (string)$v;
	    }
	    if($sign != $new_num){
	        echo json_encode(array('returncode'=>-2));
	        exit();
	    }
	    */
	    $alipay = new Alipay();
	    $re = $alipay->queryone('',$payorderid);
	    $alipay_money = $re->response->trade->total_fee;
	    if($order['money'] != $alipay_money){
	        echo json_encode(array('returncode'=>-999));
	        exit();
	    }
	    
	    switch ($paytype){
	        case 'alipay':
	            $paytypevalue = 1;
	            break;
	        default:
	            $paytypevalue = 1;
	            break;
	    }
	    $data['paytype'] = $paytypevalue;
	    $data['paytime'] = date('Y-m-d H:i:s');
	    $data['payorderid'] = $payorderid;
	    $data['status'] = 1;
	    $this->VmoneyorderTable->update($data,$order['id']);
	    /*
	    $get_money = $main['vmoney_count'] * 0.05;
	    $get_money = sprintf("%.2f", $get_money);
	    $get_money = $get_money < 0.01 ? 0.01 : $get_money;
	    */
	    $user_data['v_money'] = $user_buyer['v_money'] + $main['vmoney_count'];
	    $this->UserTable->update($user_data,$order['uid']);
	    
	    $vmoney_data['uid'] = $user_buyer['id'];
	    $vmoney_data['type'] = 3;
	    $vmoney_data['money'] = $main['vmoney_count'];
	    $vmoney_data['remark'] = '购买V币'.$main['vmoney_count'];
	    $this->VmoneyTable->add($vmoney_data);
	    /*
	    $vmoney_data['uid'] = $user_buyer['id'];
	    $vmoney_data['type'] = 1;
	    $vmoney_data['money'] = $get_money;
	    $vmoney_data['remark'] = '购买V币获得V币'.$get_money;
	    $this->VmoneyTable->add($vmoney_data);
	    
	    $vlevel_data['uid'] = $user_buyer['id'];
	    $vlevel_data['type'] = 1;
	    $vlevel_data['level'] = $get_money;
	    $vlevel_data['remark'] = '购买V币获得V级'.$get_money;
	    $this->VlevelTable->add($vlevel_data);
	    unset($vlevel_data);
	    */
	    echo json_encode(array('returncode'=>0));
	    exit();
	}
	
	protected function out()
	{
		$this->layout->nav		= 'index';
		parent::out();
	}
}

Config::extend('VmoneyController', 'Controller');
